Imports System.IO
Imports System.Net
Module Update
    Public Function CheckUpdates() As Boolean
        'Return False
        If Not My.Computer.Network.IsAvailable Then Return False

        Try
            If Not My.Computer.Network.Ping("www.halomaps.com") Then Return False
        Catch ex As System.Net.NetworkInformation.PingException
            Return False
        End Try


        Dim BaseURL As String = "http://www.halomaps.com/downloads/halo/arsenic/updates/" '"http://www.cluis.net/programs/dsm/"
        Dim currentVersion As New Version(Application.ProductVersion)

        Dim wClient As New WebClient

        Dim sReader As StreamReader
        Try
            sReader = New StreamReader(wClient.OpenRead(BaseURL & "update.txt"))
        Catch ex As WebException
            Return False
        End Try
        Dim versionString As String = sReader.ReadLine
        Dim remoteVersion As New Version(versionString)

        If remoteVersion > currentVersion Then
            Dim numFiles As Integer = Convert.ToInt32(sReader.ReadLine())
            Dim tempFiles(numFiles - 1) As String
            For x As Integer = 0 To numFiles - 1
                tempFiles(x) = sReader.ReadLine
            Next
            Dim fUpdate As New frmUpdate()
            fUpdate.Label2.Text = "Version: " & versionString
            fUpdate.TextBox1.Text = sReader.ReadToEnd
            fUpdate.Button1.Focus()
            If fUpdate.ShowDialog = DialogResult.OK Then
                If Not Directory.Exists(Application.StartupPath & "\old\") Then
                    Directory.CreateDirectory(Application.StartupPath & "\old\")
                End If
                Dim ProgressForm As New frmProgress
                ProgressForm.Text = "Updating..."
                ProgressForm.ProgressIcon = My.Resources.Arsenic48
                ProgressForm.Maximum = numFiles
                ProgressForm.Show()
                For x As Integer = 0 To numFiles - 1
                    ProgressForm.Status = "Updating " & tempFiles(x)
                    Application.DoEvents()
                    ProgressForm.Progress += 1
                    If File.Exists(Application.StartupPath & "\" & tempFiles(x)) Then
                        File.Move(Application.StartupPath & "\" & tempFiles(x), Application.StartupPath & "\old\" & tempFiles(x))
                    End If
                    wClient.DownloadFile(BaseURL & tempFiles(x), Application.StartupPath & "\" & tempFiles(x))
                Next
                ProgressForm.Close()
                ProgressForm.Dispose()
                Return True
            End If
        Else
            Return False
        End If
    End Function
End Module